package webrtc;

import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.jersey.SuspendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;

/**
 * Created by alex on 15-1-14.
 */
@Path("/channels/{topic}")
@Produces("text/html;charset=ISO-8859-1")
public class Subscriber {

    private static final Logger logger = LoggerFactory.getLogger(Subscriber.class);
    
    @GET
    public SuspendResponse<String> subscribe(
            @PathParam("topic")Broadcaster topic){
        
        return new SuspendResponse.SuspendResponseBuilder<String>()
                .broadcaster(topic)
                .outputComments(true)
                .build();
    }
    
    @POST
    public void publish(@FormParam("message") String message,@PathParam("topic") Broadcaster topic) {
        
        logger.info("Sending message {} to channel {}", new Object[]{
                message, topic.getID()
        });

        topic.broadcast(message);
    }
}
